package com.iclub.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {
    private static SimpleDateFormat yyyyDf = new SimpleDateFormat("yyyy");

    public static String date(String fmt) {
        return new SimpleDateFormat(fmt).format(new Date());
    }

    public static String date(String fmt, long t) {
        return new SimpleDateFormat(fmt).format(new Date(t));
    }

    public static String date8() {
        return new SimpleDateFormat("yyyyMMdd").format(new Date());
    }

    public static String date8(Date date) {
        return new SimpleDateFormat("yyyyMMdd").format(date);
    }

    public static String time6() {
        return new SimpleDateFormat("HHmmss").format(new Date());
    }

    public static String time6(Date date) {
        return new SimpleDateFormat("HHmmss").format(date);
    }

    public static String datetime12() {
        return new SimpleDateFormat("yyMMddHHmmss").format(new Date());
    }
    
    public static String datetime14() {
        return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    }

    public static String datetime17() {
        return new SimpleDateFormat("yyyyMMddHHmmssS").format(new Date());
    }

    public static String datetimeCH14() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }

    public static void main(String[] args) {
        System.out.println(calcDay(date8(), 30));
    }

    public static String datetime14(Date date) {
        return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
    }

    public static String datetime14(long t) {
        return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(t));
    }

    public static String calcMonth(String month6, int m) {
        Calendar cal = Calendar.getInstance();
        cal.set(Integer.parseInt(month6.substring(0, 4)), Integer
                .parseInt(month6.substring(4, 6)) - 1, 1);
        cal.add(Calendar.MONTH, m);
        return new SimpleDateFormat("yyyyMM").format(cal.getTime());
    }

    public static String calcDay(String day8, int d) {
        Calendar cal = Calendar.getInstance();
        cal.set(Integer.parseInt(day8.substring(0, 4)), Integer.parseInt(day8
                .substring(4, 6)) - 1, Integer.parseInt(day8.substring(6, 8)));
        cal.add(Calendar.DATE, d);
        return new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
    }

    public static String calcSecond(String time14, int s) {
        Calendar cal = Calendar.getInstance();
        cal.set(Integer.parseInt(time14.substring(0, 4)), Integer
                .parseInt(time14.substring(4, 6)) - 1, Integer.parseInt(time14
                .substring(6, 8)), Integer.parseInt(time14.substring(8, 10)),
                Integer.parseInt(time14.substring(10, 12)), Integer
                .parseInt(time14.substring(12, 14)));
        cal.add(Calendar.SECOND, s);
        return new SimpleDateFormat("yyyyMMddHHmmss").format(cal.getTime());
    }

    public static long toMilliSec(String time14) {
        Calendar cal = Calendar.getInstance();
        cal.set(Integer.parseInt(time14.substring(0, 4)), Integer
                .parseInt(time14.substring(4, 6)) - 1, Integer.parseInt(time14
                .substring(6, 8)), Integer.parseInt(time14.substring(8, 10)),
                Integer.parseInt(time14.substring(10, 12)), Integer
                .parseInt(time14.substring(12, 14)));
        return cal.getTimeInMillis();
    }

    public static int getActualMaximum(String day8, int field) {
        Calendar cal = Calendar.getInstance();
        cal.set(Integer.parseInt(day8.substring(0, 4)), Integer.parseInt(day8
                .substring(4, 6)) - 1, Integer.parseInt(day8.substring(6, 8)));
        return cal.getActualMaximum(field);
    }

    public static int getActualMinimum(String day8, int field) {
        Calendar cal = Calendar.getInstance();
        cal.set(Integer.parseInt(day8.substring(0, 4)), Integer.parseInt(day8
                .substring(4, 6)) - 1, Integer.parseInt(day8.substring(6, 8)));
        return cal.getActualMinimum(field);
    }

    /**
     * 得到系统时间年份
     *
     * @return
     */
    public static String getDateYear() {
        return yyyyDf.format(new Date());
    }

    /**
     * 处理银行的checkDate没有年份的情冄1�7
     *
     * @param v
     * @return
     */
    public static String formatBankCheckDate(String v) {
        if (v.length() == 4) {
            String yyyy = getDateYear();
            String stldate = yyyy + v;
            try {
                SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");

                long slttime = fmt.parse(stldate).getTime();

                long currtime = System.currentTimeMillis();

                long dis = slttime - currtime;

                long day1 = 24 * 3600 * 1000;

                if (Math.abs(dis) >= day1) {

                    if (slttime < currtime) {

                        yyyy = String.valueOf(Integer.parseInt(yyyy) + 1);

                        stldate = yyyy + v;

                    } else {

                        yyyy = String.valueOf(Integer.parseInt(yyyy) - 1);

                        stldate = yyyy + v;

                    }

                }

                return stldate;
            } catch (Exception e) {

            }
        }
        return v;

    }

    /**
	 * 
	 * description: 在now基础上增加amount个日
	 * @param endDate
	 * @param amount
	 * @return
	 */
	public static Date addDay(Date now, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.add(Calendar.DATE, amount);
		return cal.getTime();
	}
	/**
	 * 
	 * description: 在now基础上增加amount个日
	 * @param endDate
	 * @param amount
	 * @return
	 */
	public static String addDay(String now, int amount) {
		Date d ;
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
		try {
			sf.setLenient(false);
			d = sf.parse(now);
		} catch (Exception e) {
			d = new Date();
		}
		
		return sf.format(addDay(d, amount));
	}
	/**
	 * 
	 * description: 在now基础上增加amount个月
	 * @param now
	 * @param amount
	 * @return
	 */
	public static Date addMonth(Date now, int amount){
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.add(Calendar.MONTH, amount);
		return cal.getTime();
	}
	/**
	 * 
	 * description: 在now基础上增加amount个年
	 * @param endDate
	 * @param amount
	 * @return
	 */
	public static Date addYear(Date now, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.add(Calendar.YEAR, amount);
		return cal.getTime();
	}
	/**
	 * 
	 * description: 在now基础上增加amount个年
	 * @param endDate
	 * @param amount
	 * @return
	 */
	public static String addYear(String now, int amount) {
		Date d ;
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
		try {
			sf.setLenient(false);
			d = sf.parse(now);
		} catch (Exception e) {
			d = new Date();
		}
		
		return sf.format(addYear(d, amount));
	}
	/**
	 * 
	 * description: 在now基础上增加amount个分钟
	 * @param endDate
	 * @param amount
	 * @return
	 */
	public static Timestamp addMin(Timestamp now, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(now);
		cal.add(Calendar.MINUTE, amount);
		return new Timestamp(cal.getTime().getTime());
	}
	
    /**
     * String
     *
     * @param time
     * @return
     */
    public static String parseTimestampToDStr(Timestamp time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        formatter.format(time);
        return formatter.format(time);
    }

    /**
     * String
     *
     * @param time
     * @return
     */
    public static String parseTimestampToTStr(Timestamp time) {
        SimpleDateFormat formatter = new SimpleDateFormat("HHmmss");
        formatter.format(time);
        return formatter.format(time);
    }
    
    public static String parseTimestampToStr(Timestamp time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(time);
    }

    public static String formatDateTime(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    public static long getCompareDate(String startDate, String endDate)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        Date date1 = formatter.parse(startDate);
        Date date2 = formatter.parse(endDate);
        long l = date2.getTime() - date1.getTime();
        long d = l / (24 * 60 * 60 * 1000);
        return d;
    }
    public static String getCalendarString(Calendar c) {
		return c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-"
				+ c.get(Calendar.DAY_OF_MONTH);
	}
    public static Calendar getCalendarFromString(String s) {
		String[] dates = s.split("-");
		int year = Integer.parseInt(dates[0]);
		int month = Integer.parseInt(dates[1]);
		int day = Integer.parseInt(dates[2]);
		Calendar c = Calendar.getInstance();
		c.set(year, month - 1, day);
		return c;
	}


}